package com.example.demo.service.impl;

import com.example.demo.mapper.mysqlMapper.myUserMapper;
import com.example.demo.pojo.mysqlPojo.myUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = "userCache")
public class redisServiceImpl {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private myUserMapper myuserMapper;

    public myUser insertUser(myUser user){
        redisTemplate.opsForValue().set("name",user.getUsername());
        System.out.println(redisTemplate.opsForValue().get("name"));
        this.myuserMapper.insert(user);
        return this.myuserMapper.find(user.getId());
    }

    @Cacheable(key = "#p0")
    public myUser findById(String id){
        System.err.println("根据id=" + id +"获取用户对象，从数据库中获取");
        return this.myuserMapper.find(id);
    }
}
